package nmea2000

import (
	"encoding/binary"
	"fmt"

	"github.com/project-flogo/core/activity"
)

func init() {
	_ = activity.Register(&Activity{})
}

var activityMd = activity.ToMetadata(&Input{}, &Output{}, &Settings{})

// Activity is an Activity that is used to log a message to the console
// inputs : {message, flowInfo}
// outputs: none
type Activity struct {
}

// Metadata returns the activity's metadata
func (a *Activity) Metadata() *activity.Metadata {
	return activityMd
}

// Eval implements api.Activity.Eval - Logs the Message
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {

	input := &Input{}
	err = ctx.GetInputObject(input)
	if err != nil {
		return true, err
	}

	frame := input.Frame
	canID := binary.BigEndian.Uint32(frame)
	fmt.Printf("canID = %d\n", canID)
	pgn, err := GetPGN(canID)
	if err != nil {
		return true, err
	}
	output := Output{
		RawFrame: frame,
		CANID:    canID,
		PGN:      pgn.pgn,
		Prio:     pgn.prio,
		SRC:      pgn.src,
		DST:      pgn.dst,
	}
	fmt.Printf("output = %+v", output)
	err = ctx.SetOutputObject(&output)
	if err != nil {
		return true, err
	}

	return true, nil
}
